package dao.impl;

import dao.BaseDao;
import dao.IGoodsDao;
import pojo.Goods;

import java.util.List;

public class GoodsDaoImpl extends BaseDao implements IGoodsDao {
    /**
     * 查询所有商品
     *
     * @return List<Goods>
     */
    @Override
    public List<Goods> queryAllGoods() {
        return queryForList(Goods.class, "select t_goods.id,goods_name,picture,price,type_id,detail from t_goods");
    }

    /**
     * 分页查询商品
     *
     * @param pageIndex 页码，从1开始
     * @param pageSize  每页商品数量
     * @return List<Goods>
     */
    @Override
    public List<Goods> queryGoodsWithPage(int pageIndex, int pageSize) {
        return queryForList(Goods.class,
                "select t_goods.id,goods_name,picture,type_id,detail from t_goods limit ? offset ?",
                pageSize, (pageIndex - 1) * pageSize);
    }

    /**
     * 按照分类名称查询商品
     *
     * @param categoryName 分类名称
     * @return List<Goods>
     */
    @Override
    public List<Goods> queryGoodsByCategoryName(String categoryName) {
        return queryForList(Goods.class,
                "select t_goods.id,goods_name,picture,price,type_id,detail from t_goods,t_category where t_goods.type_id=t_category.id and category=? ",
                categoryName);
    }

    /**
     * 按照分类名称查询商品并分页
     *
     * @param categoryName 分类名称
     * @param pageIndex    页码，从1开始
     * @param pageSize     每页商品数量
     * @return List<Goods>
     */
    @Override
    public List<Goods> queryGoodsByCategoryNameWithPage(String categoryName, int pageIndex, int pageSize) {
        return queryForList(Goods.class,
                "select t_goods.id,goods_name,picture,price,type_id,detail from t_goods,t_category where t_goods.type_id=t_category.id and category=? limit ? offset ?",
                categoryName, pageSize, (pageIndex - 1) * pageSize);
    }

    /**
     * 按照分类id查询商品
     *
     * @param categoryId 分类id
     * @return List<Goods>
     */
    @Override
    public List<Goods> queryGoodsByCategoryId(int categoryId) {
        return queryForList(Goods.class,
                "select t_goods.id,goods_name,picture,price,type_id,detail from t_goods,t_category where t_goods.type_id=t_category.id and type_id=? ",
                categoryId);
    }

    /**
     * 根据商品名字模糊查询商品
     *
     * @param goodsName 商品名字
     * @return List<Goods>
     */
    @Override
    public List<Goods> fuzzyQueryGoodsByName(String goodsName) {
        return queryForList(Goods.class,
                "select id,goods_name,picture,price,type_id,detail from t_goods where locate(?,goods_name)!=0",
                goodsName);

    }

    /**
     * 根据商品id查询商品
     *
     * @param id 商品id
     * @return Goods
     */
    @Override
    public Goods queryGoodsById(int id) {
        return queryForOne(Goods.class,
                "select id,goods_name,picture,price,type_id,detail from t_goods where id=?",
                id);
    }
}
